import collections


class Solution(object):
    def crackSafe(self, n, k):
        if n == 1:
            ans = [str(i) for i in range(k)]
            return ''.join(ans)
        ans = []
        node = []
        d = collections.defaultdict(lambda: {str(i) for (i) in range(k)})

        def dfs(node):
            next_node = node[1:]
            while d[node]:
                v = d[node].pop()
                dfs(tuple(next_node + (v,)))
            ans.append(node[-1])

        for _ in range(n - 2):
            node.append('0')
        node.append('0')
        node = tuple(node)
        dfs(node)
        for _ in range(n - 2):
            ans.append('0')
        return ''.join(ans)


data = Solution()
n = 3
k = 2
print(data.crackSafe(n, k))
